<?php
include_once '../inc/config_inc.php';
include_once '../inc/mysql_inc.php';
include_once '../inc/tool_inc.php';
$link=connect();
if(!isset($_GET['id']) || !is_numeric($_GET['id'])){
	skip('life.php','error','id参数错误！');exit();
}
$query="select * from life where id={$_GET['id']}";
$result=execute($link,$query);
if(!mysqli_num_rows($result)){
	skip('life.php','error','这条学习笔记信息不存在！');exit();
}
if(isset($_POST['submit'])){
	//验证用户填写的信息
	$_POST=escape($link,$_POST); //转义
	$query="update life set title='{$_POST['title']}',content='{$_POST['content']}',sort={$_POST['sort']} where id={$_GET['id']}";
	execute($link,$query);
	if(mysqli_affected_rows($link)==1){
		skip('life.php','ok','修改成功！');exit();
	}else{
		skip('life.php','error','修改失败,请重试！');exit();
	}
}
$data=mysqli_fetch_assoc($result);

$template['title']='修改慢生活'; 
$template['css']=array('style/public.css');
?>

<?php include 'inc/header_inc.php'; ?>

<div id="main">
	<div class="title" style="margin-bottom:20px;">修改慢生活</div>
	<form method="post" onsubmit="return check()">
		<table class="au">
			<tr>
				<td>标题</td>
				<td><input name="title" value="<?php echo $data['title']?>" type="text" id="title" /></td>
				<td>
					标题不得为空，最大不得超过20个字符
				</td>
			</tr>
			<tr>
				<td>内容</td>
				<td><input name="content" value="<?php echo $data['content']?>" type="text" id="content" /></td>
				<td>
					内容不得为空，最大不得超过1000个字符
				</td>
			</tr>
			<tr>
				<td>排序</td>
				<td><input name="sort" value="<?php echo $data['sort']?>" type="text" id="sort"/></td>
				<td>
					填写一个数字即可
				</td>
			</tr>
		</table>
		<input style="margin-top:20px;cursor:pointer;" class="btn" type="submit" name="submit" value="修改" />
	</form>
<script type="text/javascript">
	function check(){
		var t=document.getElementById('title').value;
		var c=document.getElementById('content').value;
		var s=document.getElementById('sort').value;
		if(t===""){
			alert("标题不能为空!");
			return false;
		}else{
			if(t.length>20){
				alert("最大不得超过20个字符!");
				return false;
			}
		}
		if(c===""){
			alert("内容不能为空!");
			return false;
		}else{
			if(c.length>1000){
				alert("最大不得超过1000个字符!");
				return false;
			}
		}
		if(s===""){
			alert("排序不得为空!");
			return false;
		}else{
			if(isNaN(s)){
				alert("排序只能是数字!");
				return false;
			}
		}
	}
</script>
</div>

<?php include 'inc/footer_inc.php'; ?>